package com.ding.ding

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Instrumentation
import android.content.ContentValues.TAG
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.util.Log.e
import android.util.Log.i
import android.view.KeyEvent
import android.view.MotionEvent
import com.ding.ding.constant.AppConstant
import java.io.DataOutputStream
import java.text.SimpleDateFormat
import java.util.*


class MainActivity : Activity() {
    @SuppressLint("SimpleDateFormat")
    val simpleDateFormat = SimpleDateFormat("HHmmss")

    private var boolean = true
    private var handlerAuto: Handler = Handler()
    private var nowTime = 0
    private var handlerGohome: Handler = Handler()
    private fun goHome() {
        handlerGohome.postDelayed({
            val intent2 = Intent(Intent.ACTION_MAIN)
            intent2.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            intent2.addCategory(Intent.CATEGORY_HOME)
            startActivity(intent2)
        }, 1000 * 10)
    }

    private fun startDingDIng() {
        handlerAuto.postDelayed({
            val date = Date()
            val nowTime = simpleDateFormat.format(date)
            i("nowTime HHmmss -->", nowTime)
            if (nowTime.toInt() in 70001..83000) {
                //if (nowTime.toInt() in 172901..232323) {
                i("上班打卡-->", nowTime)
                DingKit.openDing("com.alibaba.android.rimet", this)
                goHome()
            }

            if (nowTime.toInt() in 173010..173200) {
                i("下班打卡-->", nowTime)
                DingKit.openDing("com.alibaba.android.rimet", this)
                goHome()
            }
            startDingDIng()
        }, 1000 * 20)
    }

    private fun refreshBoolean() {
        handlerAuto.postDelayed({
            val date = Date()
            val nowTime = simpleDateFormat.format(date)
            i("nowTime HHmmss -->", nowTime)
            if (nowTime.toInt() in 70001..83000) {
                //if (nowTime.toInt() in 172901..232323) {
                i("上班打卡-->", nowTime)
                val intent = Intent(Intent.ACTION_VIEW)
                intent.data = Uri.parse(AppConstant.SCHEME_DING_DING)
                startActivity(intent)
            }

            if (nowTime.toInt() in 172801..173900) {
                i("下班打卡-->", nowTime)
                val intent = Intent(Intent.ACTION_VIEW)
                intent.data = Uri.parse(AppConstant.SCHEME_DING_DING)
                startActivity(intent)
            }

            refreshBoolean()
        }, 2000 * 10)
    }

    private var handlerAuto2: Handler = Handler()

    private fun refreshHome() {
        i("refreshHome--->", "refreshHome")
        handlerAuto2.postDelayed({
            try {
                object : Thread() {
                    override fun run() {
                        try {
                            val inst = Instrumentation()
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK)
                        } catch (ee: Exception) {
                            e("Exception when onBack", ee.toString())
                        }

                    }
                }.start()

//                i("refreshHome--->", "refreshHome")
//                val intent2 = Intent(Intent.ACTION_MAIN)
//                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                //intent2.addCategory(Intent.ACTION_USER_BACKGROUND)
//                intent2.addCategory(Intent.CATEGORY_HOME)
////                intent2.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER)
////                intent2.addCategory(Intent.ACTION_USER_BACKGROUND)
//                startActivity(intent2)
            } catch (e: Exception) {
            }
            refreshHome()
        }, 5000)
    }


    private fun refreshBack() {
//        handlerAuto.postDelayed({
//            refreshBack()
//        }, 1500 * 10)
    }

    private fun openActivity() {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse(AppConstant.SCHEME_DING_DING)
        startActivity(intent)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        startDingDIng()
//        refreshClick()
//        runRootCommand()
//        setContentView(R.layout.activity_main)
////        autoCheckRfidHandler.postDelayed({
////            mPresenter.getRfidDeviceScanList(now_check_rfid_type)
////        }, now_check_rfid_del_time)
//        refreshBoolean()
//        refreshBack()
//        refreshHome()
//        open_dingding.setOnClickListener {
//            //            openActivity()
//        }
//
//        // 支付宝付款码的 scheme
//        open_zhifubao_shoukuan.setOnClickListener {
//            val intent = Intent(Intent.ACTION_VIEW)
//            intent.data = Uri.parse(AppConstant.SCHEME_ALIPAY_PAY)
//            startActivity(intent)
//        }
//
//        // 支付宝扫一扫 scheme
//        open_saoyisao.setOnClickListener {
//            val intent = Intent(Intent.ACTION_VIEW)
//            intent.data = Uri.parse(AppConstant.SCHEME_ALIPAY_SCAN)
//            startActivity(intent)
//        }


    }

    private fun refreshClick() {
        i("refreshClick--->", "refreshClick")
        handlerAuto2.postDelayed({
            try {
                val evenDownt = MotionEvent.obtain(
                    System.currentTimeMillis(), System.currentTimeMillis() + 100,
                    MotionEvent.ACTION_DOWN, 100f, 400f, 0
                )
                dispatchTouchEvent(evenDownt)
                val eventUp = MotionEvent.obtain(
                    System.currentTimeMillis(), System.currentTimeMillis() + 100,
                    MotionEvent.ACTION_UP, 100f, 400f, 0
                )
                dispatchTouchEvent(eventUp)
                evenDownt.recycle()
                eventUp.recycle()
            } catch (e: Exception) {
            }
            refreshClick()
        }, 1000)
    }

    override fun onBackPressed() {
    }

    fun runRootCommand(command: String = "ROOT"): Boolean {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process!!.outputStream)
            os.writeBytes(command + "\n")
            os.writeBytes("exit\n")
            os.flush()
            process.waitFor()
        } catch (e: Exception) {
            Log.d(TAG, "the device is not rooted,  error message： " + e.message)
            return false
        } finally {
            try {
                if (os != null) {
                    os.close()
                }
                process?.destroy()
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        return true
    }


}